
//设置html根目录字体大小
require(['jquery','../modules/pageSize'],function($,pageSetSize){
    setRootFontSize(pageSetSize);
});

//页面渲染
require(['jquery','../modules/popup'],function($,popup){
    /*初始化*/
    var counter = 0; /*计数器*/
    var pageStart = 0; /*offset*/
    var pageSize = 10; /*size*/
    var isEnd = false;/*结束标志*/
    var isAjax = false;/*防止滚动过快，服务端没来得及响应造成多次请求*/
	var _ycMarkArr = ["闪购", "促销", "套餐"];
    /*首次加载*/
    getData(pageStart, pageSize,page);

    /*监听加载更多*/
    $(window).scroll(function(){
        //console.log(isEnd, isAjax);
        /*滚动加载时如果已经没有更多的数据了、正在发生请求时，不能继续进行*/
        if(isEnd == true || isAjax == true){
            return;
        }
        // 当滚动到最底部以上100像素时， 加载新内容
        if ($(document).height() - $(this).scrollTop() - $(this).height()<10){
            counter ++;
            pageStart = counter * pageSize;
            //console.log((counter+page));
            getData(pageStart, pageSize,(counter+page));
        }
    });
    function getData(offset,size,page){
        isAjax = true;
        $.ajax({
            type: 'GET',
            url: '/home/store/ajax_goods_list'+getFixedExtraUrl(),
            data: {
                local_id: $local_id,
                id: $id,
                type: $type,
                p:page
            },
            dataType: 'json',
            beforeSend: function () {
                $("body").append('<div id="pload" style="position:fixed;top:0;z-index:1200;background: rgba(0,0,0,0.3);width:100%;height:100%;margin:auto auto;">' +
                    '<p style="width:2.0rem;height: 2.0rem; margin: 0 auto;text-align: center; position: relative; top: 5rem;">' +
                    '<span style="font-size: .32rem;color: #3c6a63;">加载中...</span>' +
                    '<img src="/Public/Home/images/loading-more.gif" style="display: block;width: .32rem;height: .32rem; margin: 0 auto;"></p>'+
                    '</div>');
            },
            success: function(str){
                isAjax = false;
                var data = str.data,
                    sum = str.data.length,
                    page = str.page,
                    totalPage = str.totalPage,
                    result = '';
                var res_cur='';
                /************业务逻辑块：实现拼接html内容并append到页面*****************/

                //console.log(data , sum, page,totalPage,result);
                /*如果剩下的记录数不够分页，就让分页数取剩下的记录数
                 * 例如分页数是5，只剩2条，则只取2条
                 *
                 * 实际MySQL查询时不写这个不会有问题
                 */
                if(sum - offset < size ){
                    size = sum - offset;
                }
                /*使用for循环模拟SQL里的limit(offset,size)*/
                for (var i = 0; i < sum; i++) {
                    result += '<div class="goods-item clearfix">';
                    var store_cur = data[i].store, store_last = data[i].freez;
                    var store_out = parseInt(store_cur - store_last);
					// 0: 闪购 1：促销 2：组合促销
					var g_mark = -1;
					var g_price = "";
					var _link = '/goods/detail/id/'+data[i].store_pid+'/local_id/'+data[i].local_id+'/top_local_id/'+yc_pass_arr.top_local_id;
					if(!!data[i].flashPrice){
						g_mark = 0;
						g_price = data[i].flashPrice;								
					}else if(!!data[i].proPrice){
						g_mark = 1;
						g_price = data[i].proPrice;									
					}else if(data[i].groupPromotion){
						g_mark = 2;
					}
					result += '<a href="'+_link+'" class="img '+(store_out > 0 ? "" : "sold_out")+'"><img src="'+getDefaultImg(data[i].image)+'"></a>';
                    result += '<div class="text">';
                    result += '<a href="'+_link+'" class="goods-name">'+ data[i].name + '</a>';
					if(data[i].spec_info != null ){
                        result += '<p class="des text-hide">' + data[i].spec_info + '</p>';
                    }
					if(g_mark != -1){
						result += '<p class="gmarks"><i class="o-mark">'+_ycMarkArr[g_mark]+'</i></p>';	
					}
					if(g_mark == 0){
						result += '<p class="clearfix"><label class="sale-price">￥' + g_price + '</label></p>';
					}else if(g_mark == 1 && parseFloat(g_price) != parseFloat(data[i].price)){
						result += '<p class="clearfix"><label class="sale-price">￥' + g_price + '</label><label class="old-price">￥' + data[i].price + '</label></p>';
					}else {
						result += '<p class="clearfix"><label class="sale-price">￥' + data[i].price + '</label></p>';
					}
                    
                    result += '</div>';
                    result += '</div>';
                }
                $('.goods-horizontal').append(result);
                /*******************************************/
                if ( page  >= totalPage){
                    res_cur +='<div class="text" style="font-size: .26rem; padding:.15rem 0; text-align: center;line-height: 1.5;color:#787878; ">已经显示完全所有商品</div>';
                    $('.goods-horizontal').append(res_cur);
                    isEnd = true;
                }
            },
            complete: function () {
                $("#pload").remove();
            },
            error: function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        });
    }
});
